package com.alibaba.citrus.ots.price.ability.functions.factory.price.process;

import com.alibaba.acm.shaded.com.google.common.collect.Lists;
import com.alibaba.citrus.ots.common.annotation.ProcessInvoker;
import com.alibaba.citrus.ots.common.annotation.RepositoryInvoker;
import com.alibaba.citrus.ots.common.exception.RepositoryException;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.bcots.model.dto.ScItemSDO;
import com.epoch.app.bcots.model.dto.SearchScItemRequest;
import com.epoch.app.bcots.service.BaseDataService;
import com.epoch.app.otspricecenter.enums.DimensionPropertyEnum;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author hanguo
 * @date 2021/11/19
 * @Description 货品
 */
@Component
public class ScItemProcess extends PriceProcess {
    /**
     * 基线能力
     */
    @Resource
    private BaseDataService baseDataService;

    //货品一次查询量
    private static Integer pageSize = 2000;

    @Override
    @RepositoryInvoker
    public Map<String, String> obtainCodeAndDescMap(List<String> codeList, boolean innerCode) {
        List<ScItemSDO> scItemSDOS = obtainScItemSDOList(codeList, innerCode);
        if (CollectionUtils.isEmpty(scItemSDOS)) {
            return new HashMap<>();
        }
        if (innerCode) {
            //内部编码:描述信息
            return scItemSDOS.stream().filter(e -> StringUtils.isNotBlank(e.getScItemId())
                            && StringUtils.isNotBlank(e.getTitle()))
                    .collect(Collectors.toMap(ScItemSDO::getScItemId, ScItemSDO::getTitle, (x, y) -> x));
        }
        //外部编码:描述信息
        return scItemSDOS.stream().filter(e -> StringUtils.isNotBlank(e.getOuterId())
                        && StringUtils.isNotBlank(e.getTitle()))
                .collect(Collectors.toMap(ScItemSDO::getOuterId, ScItemSDO::getTitle, (x, y) -> x));

    }

    @Override
    @RepositoryInvoker
    public Map<String, String> obtainInnerCodeAndOuterCodeMap(List<String> codeList, boolean innerCode) {
        List<ScItemSDO> scItemSDOS = obtainScItemSDOList(codeList, innerCode);
        if (CollectionUtils.isEmpty(scItemSDOS)) {
            return new HashMap<>();
        }
        if (innerCode) {
            //内部编码:外部编码
            return scItemSDOS.stream().filter(item -> StringUtils.
                            isNotBlank(item.getScItemId()) && StringUtils.isNotBlank(item.getOuterId())).
                    collect(Collectors.toMap(ScItemSDO::getScItemId, ScItemSDO::getOuterId, (x, y) -> y));
        }
        //外部编码:内部编码
        return scItemSDOS.stream().filter(item -> StringUtils.
                        isNotBlank(item.getOuterId()) && StringUtils.isNotBlank(item.getScItemId())).
                collect(Collectors.toMap(ScItemSDO::getOuterId, ScItemSDO::getScItemId, (x, y) -> y));
    }

    @ProcessInvoker
    public List<ScItemSDO> obtainScItemSDOList(List<String> codeList,boolean innerCode){

        List<ScItemSDO> scItemSDOS = Lists.newArrayList();

        codeList = dealCodeList(codeList);
        if(Objects.isNull(codeList)){
            return scItemSDOS;
        }

        SearchScItemRequest searchScItemRequest = new SearchScItemRequest();
        searchScItemRequest.setPageSize(pageSize);
        Map<String, Object> searchReqDTO = new HashMap<>();
            if (innerCode) {
                searchReqDTO.put("scItemIds", codeList.stream().filter(item -> StringUtils.isNotEmpty(item)).collect(Collectors.toList()));
            } else {
                searchReqDTO.put("outerIdList", codeList.stream().filter(item -> StringUtils.isNotEmpty(item)).collect(Collectors.toList()));
            }
            searchScItemRequest.setSearchReqDTO(searchReqDTO);
            Result<List<ScItemSDO>> scItemListResult = baseDataService.searchScItem(searchScItemRequest);
            if (Objects.isNull(scItemListResult)) {
                throw new RepositoryException("OTS-07-010-01-17-001");
            }
            if (CollectionUtils.isEmpty(scItemListResult.getResult())) {
                return scItemSDOS;
            }

        return scItemListResult.getResult();
    }

    @Override
    public String getPropertyCode() {
        return DimensionPropertyEnum.MATERIAL_CODE.getKey();
    }
}
